#include "ResetFactoryWorker.h"
#include <unistd.h>
#include <fcntl.h>
#include "BootSetting.h"

ResetFactoryWorker *ResetFactoryWorker::getInstance()
{
    static ResetFactoryWorker singleton{};
    return &singleton;
}

void ResetFactoryWorker::startWorkSlot()
{
    start(LowPriority);
}

ResetFactoryWorker::ResetFactoryWorker()
{

}

void ResetFactoryWorker::work()
{
    RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.isFirstTimeLogin = 0;
    RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.languageIndex = 0;
    RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.loginMode = 0;
    RuntimeData::BootSetting::getInstance()->saveBootSettingInfoToRom();

#ifdef arm
    QStringList cmd;
    cmd << QString("rm /home/root/data/* -rf");
    cmd << QString("rm /home/root/config/* -rf");
    cmd << QString("rm /home/root/applog.txt -rf");
    cmd << QString("rm /home/root/database/database.db -rf");
    QString command = cmd.join(";");
    qDebug()<<"export cmd:"<<command;

    int status = system(command.toUtf8().data());
    if((status < 0) || (!WIFEXITED(status)) || (0 != WEXITSTATUS(status))) {
        qDebug()<<"ResetFactoryWorker exe failed";
        emit exeErrorSignal();
        return;
    }
    sync();
    sleep(1);
#else
    QThread::msleep(5000);/* 测试 */
#endif
}

void ResetFactoryWorker::run()
{
    qDebug()<<"ResetFactoryWorker running";
    work();
    emit completeWorkSignal();
}
